package com.yndsoft.jc.utils.permission;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/8/5.
 */

public class NewsShowTimeUtil {

    private Timer timer;
    private TimerTask task;
    private TextView newsCount;

    /**
     * 按钮点击倒计时
     */
    public void countTime() {
        timer = new Timer();
        task = new TimerTask() {

            @Override
            public void run() {
                Message msg = Message.obtain();
                msg.what = 1;
                handler.sendMessage(msg);
            }
        };
        timer.schedule(task, 2500);
    }

    public NewsShowTimeUtil(TextView newsCount) {
        this.newsCount = newsCount;
    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 1:
                    newsCount.setVisibility(View.GONE);
                    if (null != timer && null != task) {
                        timer.cancel();
                        task.cancel();
                    }
                    timer = null;
                    task = null;
                    break;
            }
        }
    };

    public void cancelTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (task != null) {
            task.cancel();
            task = null;
        }
    }
}
